from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.logs.models import Log
from django.contrib.auth.models import User

@receiver(post_save, sender=User)
def log_user_save(sender, instance, created, **kwargs):
    """
    在用户创建或更新时记录日志
    """
    action = "Created" if created else "Updated"
    Log.objects.create(
        log_type='AUDIT',
        user=instance,
        action=f'User {action}',
        related_object=f"UserID:{instance.id}",
        additional_data={"username": instance.username}
    )
